package org.calculator.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

public class LayoutParser {
	private InputStream inputStream;

	public LayoutParser(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public List<String> parse() throws IOException, SAXException {
		final List<String> lista = new LinkedList<String>();

		RootElement root = new RootElement("LinearLayout");
		Element button = root.getChild("Button");

		button.setStartElementListener(new StartElementListener() {
			public void start(Attributes attrs) {
				if (attrs.getValue(0) != null)
					lista.add(new String(attrs.getValue("android:id")));
				// if (attrs.getValue("android:id") != null)
				// lista.add(new String(attrs.getValue("android:id")));
				else
					lista.add(new String("NO_ID"));
			}
		});
		// Xml.parse(fileReader, root.getContentHandler());
		Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());

		return lista;
	}
}